package com.cyl.XmlDemo.web;

import java.security.Principal;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.annotation.SendToUser;
import org.springframework.messaging.simp.annotation.SubscribeMapping;
import org.springframework.stereotype.Controller;

import com.cyl.XmlDemo.entity.Shout;

//@Controller
public class MarcoController {
	
	private final static Logger logger = LoggerFactory.getLogger(MarcoController.class);
	
	@MessageMapping("/marco-who")
	@SendToUser(value="/topic/polo-who",broadcast=false)
	public Shout handleUser(Shout shout){
		/*logger.debug("marco-who 被触发" + principal.getName());*/
		Shout s = new Shout();
		s.setMessage("当前用户：");
		return s;
	}
	
	@MessageMapping("/marco-all")
	@SendTo(value="/topic/polo-who")
	public Shout handleAllUser(Shout shout){
		logger.debug("marco-all 被触发" + shout.getMessage());
		Shout s = new Shout();
		s.setMessage("当前用户：");
		return s;
	}
	
	@MessageMapping("/marco")
	public void handleShout(Shout incoming){
		logger.debug("Received message: " + incoming.getMessage());
	}
	
	@MessageMapping("/marco-s")
	public Shout handleShoutReturnMsg(Shout incoming){
		logger.debug("S Received message: " + incoming.getMessage());
		Shout shout = new Shout();
		shout.setMessage("Polo!");
		return shout;
	}
	
	@SubscribeMapping("marco-server")
	public Shout handleSubMsg(){
		logger.debug("marco-server 被订阅");
		Shout shout = new Shout();
		shout.setMessage("Polo! 订阅信息成功");
		return shout;
	}
	
	@SubscribeMapping("/marcosub")
	public Shout handlerSubscription(){
		logger.debug("subscribe 被触发");
		Shout shout = new Shout();
		shout.setMessage("Polo!");
		return shout;
	}
	
/*	@MessageExceptionHandler
	public void handlerException(Throwable t){
		logger.debug(t.getMessage());
		t.getStackTrace();
	}*/
}
